var app = getApp();
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var util = require('../../utils/util.js');
var MPage = require('../MPage.js');

Page(common.extendPage(MPage, {
	data: {
		dataLoaded: false,
		loading: false,
		moreLoading: false,
		currentTab: 0,
		orderList: [],
		page: 1,
		noMore: false,
	},

	onLoad: function(options) {
		var self = this;

		self.techId = options.techId;
		common.initApp({
			loginSuc() {
				self._toTab(+options.ty || 0, function() {
					self.setData({
						dataLoaded: true,
					});
				});
			}
		});
	},

	_stopProp() {
		return false;
	},

	/**
	 * 事件切换tab
	 */
	_tabSwitch: function(evt) {
		var index = +evt.currentTarget.dataset.current;

		this._toTab(index);
	},

	_toTab: function(index, cb) {
		this.setData({
			currentTab: index,
			orderList: [],
			page: 1,
			noMore: false,
		});
		this._getOrderList({
			ty: index
		}, cb);
	},

	_getOrderList: function(params, cb) {
		var self = this;
		var ty = params.ty;
		var isMore = params.isMore;
		var page = self.data.page || 1;
		var url = '/openapi/CTechnician/userBespeakList';

		if (self.techId && self.techId != 0) {
			url = '/openapi/CTechnician/techBespeakList';
		}

		if (isMore) {
			self.setData({
				moreLoading: true
			});
		} else {
			self.setData({
				loading: true
			});
		}
		ajax.G(url, {
			page: page,
			state: ty
		}, function(data) {
			var list = data.result || [];

			list = list.map(function(item) {
				var tech = item['technician'];
				var payState = item.pay_state;
				var obj = {
					id: item.id,
					state: item.state,
					payState,
					stateName: payState == 0 ? '待支付' : item.stateName,
					createTime: item.createTime,
					appointTime: item.date + ' ' + item.beginTime + '-' + item.endTime,
					money: item.pre_price,
				};

				if (self.techId && self.techId != 0) {
					Object.assign(obj, {
						userHeadImg: item['userHead'],
						userName: item['username'],
						userPhone: item['userphone'],
						remark: item.remark,
					});
				} else {
					Object.assign(obj, {
						techHeadImg: tech['head_img'],
						techName: tech['name'],
						techTitle: tech['title'],
						techPhone: tech['phone'],
					});
				}
				return obj;
			});

			var toData = {
				techId: self.techId,
				orderList: list.concat(self.data.orderList),
				noMore: list.length == 0,
				page: self.data.page + 1,
			};
			toData[isMore ? 'moreLoading' : 'loading'] = false;
			self.setData(toData);
			cb && cb();
		});
	},

	/**
	 * 加载更多
	 */
	_loadMore: function() {
		if (this.data.moreLoading || this.data.loading || this.data.noMore) {
			return;
		}
		this._getOrderList({
			ty: this.data.currentTab,
			isMore: true
		});
	},

	/**
	 * 取消预约
	 */
	cancelAppoint(evt) {
		var _slef = this;
		var {
			orderId
		} = evt.currentTarget.dataset

		util.confirm('取消预约将退款给客户，您确定取消预约吗？', function() {
			ajax.P('/openapi/CTechnician/changeBespeakState', {
				id: orderId,
				state: 3,
			}, function(data) {
				let orderList = _slef.data.orderList || [];

				orderList.some(function(order, index) {
					if (order.id == orderId) {
						orderList.splice(index, 1);
						return true;
					}
				});
				_slef.setData({
					orderList
				});
			});
		});
		common.submitForm(evt.detail.formId);
	},

	/**
	 * 接受预约
	 * @return {[type]} [description]
	 */
	acceptAppoint(evt) {
		var _slef = this;
		var {
			orderId
		} = evt.currentTarget.dataset

		ajax.P('/openapi/CTechnician/changeBespeakState', {
			id: orderId,
			state: 1,
		}, function(data) {
			let orderList = _slef.data.orderList || [];

			orderList.some(function(order, index) {
				if (order.id == orderId) {
					orderList.splice(index, 1);
					return true;
				}
			});
			_slef.setData({
				orderList
			});
			util.alert('接受预约成功！');
		});
		common.submitForm(evt.detail.formId);
	},

	/**
	 * 完成服务
	 * @return {[type]} [description]
	 */
	completeAppoint(evt) {
		var _slef = this;
		var {
			orderId
		} = evt.currentTarget.dataset

		util.confirm('您确定要执行完成服务操作吗？', function() {
			ajax.P('/openapi/CTechnician/changeBespeakState', {
				id: orderId,
				state: 2,
			}, function(data) {
				let orderList = _slef.data.orderList || [];

				orderList.some(function(order, index) {
					if (order.id == orderId) {
						orderList.splice(index, 1);
						return true;
					}
				});
				_slef.setData({
					orderList
				});
				util.alert('完成服务成功！');
			});
		});
		common.submitForm(evt.detail.formId);
	},

	_callUser(evt) {
		var {
			userPhone
		} = evt.currentTarget.dataset

		wx.makePhoneCall({
			phoneNumber: userPhone
		});
		common.submitForm(evt.detail.formId);
	},

}));